home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / PickText3D / PickText3DGeometry.java.z / PickText3DGeometry.java
Encoding:
Java Source  |  2003-08-08  |  8.6 KB  |  246 lines

  1. /*
  2.  *    @(#)PickText3DGeometry.java 1.14 02/10/21 13:49:47
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.BorderLayout;
  42. import com.sun.j3d.utils.picking.PickTool;
  43. import com.sun.j3d.utils.picking.behaviors.*;
  44. import com.sun.j3d.utils.geometry.Sphere;
  45. import com.sun.j3d.utils.applet.MainFrame;
  46. import com.sun.j3d.utils.universe.*;
  47. import javax.media.j3d.*;
  48. import javax.vecmath.*;
  49. import java.util.Enumeration;
  50. import java.awt.*;
  51. import java.lang.String;
  52.  
  53. public class PickText3DGeometry extends Applet {
  54.  
  55.     private SimpleUniverse u = null;
  56.  
  57.   public BranchGroup createSceneGraph(Canvas3D canvas) {
  58.     Color3f eColor    = new Color3f(0.0f, 0.0f, 0.0f);
  59.     Color3f sColor    = new Color3f(1.0f, 1.0f, 1.0f);
  60.     Color3f objColor  = new Color3f(0.6f, 0.6f, 0.6f);
  61.     Color3f lColor1   = new Color3f(1.0f, 0.0f, 0.0f);
  62.     Color3f lColor2   = new Color3f(0.0f, 1.0f, 0.0f);
  63.     Color3f alColor   = new Color3f(0.2f, 0.2f, 0.2f);
  64.     Color3f bgColor   = new Color3f(0.05f, 0.05f, 0.2f);
  65.     
  66.     Transform3D t;
  67.     
  68.     // Create the root of the branch graph
  69.     BranchGroup objRoot = new BranchGroup();
  70.  
  71.     // Create a Transformgroup to scale all objects so they
  72.     // appear in the scene.
  73.     TransformGroup objScale = new TransformGroup();
  74.     Transform3D t3d = new Transform3D();
  75.     t3d.setScale(0.4);
  76.     objScale.setTransform(t3d);
  77.     objRoot.addChild(objScale);
  78.     
  79.     // Create a bounds for the background and lights
  80.     BoundingSphere bounds =
  81.       new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  82.     
  83.     // Set up the background
  84.     Background bg = new Background(bgColor);
  85.     bg.setApplicationBounds(bounds);
  86.     objScale.addChild(bg);
  87.     
  88.     Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
  89.     Appearance a = new Appearance();
  90.     m.setLightingEnable(true);
  91.     a.setMaterial(m);
  92.     Font3D f3d = new Font3D(new Font("TestFont", Font.PLAIN, 1),
  93.                 new FontExtrusion());
  94.  
  95.     Text3D text3D = new Text3D(f3d, new String("TEXT3D"),
  96.                 new Point3f(-2.0f, 0.7f, 0.0f));
  97.     text3D.setCapability(Geometry.ALLOW_INTERSECT);
  98.     Shape3D s3D1 = new Shape3D();
  99.     s3D1.setGeometry(text3D);
  100.     s3D1.setAppearance(a);
  101.  
  102.     // Create a transform group node and initialize it to the
  103.     // identity.  Enable the TRANSFORM_WRITE capability so that
  104.     // our behavior code can modify it at runtime.
  105.     TransformGroup spinTg1 = new TransformGroup();
  106.     spinTg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  107.     spinTg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  108.     spinTg1.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
  109.  
  110.     spinTg1.addChild(s3D1);
  111.     objScale.addChild(spinTg1);
  112.  
  113.     Text3D pick = new Text3D(f3d, new String("Pick me"),
  114.                 new Point3f(-2.0f, -0.7f, 0.0f));
  115.     pick.setCapability(Geometry.ALLOW_INTERSECT);
  116.     Shape3D s3D2 = new Shape3D();
  117.     s3D2.setGeometry(pick);
  118.     s3D2.setAppearance(a);
  119.  
  120.     // Create a transform group node and initialize it to the
  121.     // identity.  Enable the TRANSFORM_WRITE capability so that
  122.     // our behavior code can modify it at runtime.
  123.     TransformGroup spinTg2 = new TransformGroup();
  124.     spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  125.     spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  126.     spinTg2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
  127.  
  128.     spinTg2.addChild(s3D2);
  129.     objScale.addChild(spinTg2);
  130.     
  131.     // Create the transform group node for the each light and initialize
  132.     // it to the identity.  Enable the TRANSFORM_WRITE capability so that
  133.     // our behavior code can modify it at runtime.  Add them to the root
  134.     // of the subgraph.
  135.  
  136.     // Create transformations for the positional lights
  137.     t = new Transform3D();
  138.     Vector3d lPos1 =  new Vector3d(0.0, 0.0, 2.0);
  139.     t.set(lPos1);
  140.     TransformGroup l1Trans = new TransformGroup(t);
  141.     l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  142.     l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  143.     l1Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
  144.     objScale.addChild(l1Trans);
  145.     
  146.     t = new Transform3D();
  147.     Vector3d lPos2 = new Vector3d(0.5, 1.2, 2.0);
  148.     t.set(lPos2);
  149.     TransformGroup l2Trans = new TransformGroup(t);
  150.     l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  151.     l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  152.     l2Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
  153.     objScale.addChild(l2Trans);
  154.  
  155.     // Create Geometry for point lights
  156.     ColoringAttributes caL1 = new ColoringAttributes();
  157.     ColoringAttributes caL2 = new ColoringAttributes();
  158.     caL1.setColor(lColor1);
  159.     caL2.setColor(lColor2);
  160.     Appearance appL1 = new Appearance();
  161.     Appearance appL2 = new Appearance();
  162.     appL1.setColoringAttributes(caL1);
  163.     appL2.setColoringAttributes(caL2);
  164.     l1Trans.addChild(new Sphere(0.05f,
  165.     Sphere.GENERATE_NORMALS | Sphere.ENABLE_GEOMETRY_PICKING, 15, appL1));
  166.     l2Trans.addChild(new Sphere(0.05f,
  167.     Sphere.GENERATE_NORMALS | Sphere.ENABLE_GEOMETRY_PICKING, 15, appL2));
  168.     
  169.     // Create lights
  170.     AmbientLight aLgt = new AmbientLight(alColor);
  171.     
  172.     Light lgt1;
  173.     Light lgt2;
  174.  
  175.     Point3f lPoint  = new Point3f(0.0f, 0.0f, 0.0f);
  176.     Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
  177.     lgt1 = new PointLight(lColor1, lPoint, atten);
  178.     lgt2 = new PointLight(lColor2, lPoint, atten);
  179.     
  180.     // Set the influencing bounds
  181.     aLgt.setInfluencingBounds(bounds);
  182.     lgt1.setInfluencingBounds(bounds);
  183.     lgt2.setInfluencingBounds(bounds);
  184.     
  185.     // Add the lights into the scene graph
  186.     objScale.addChild(aLgt);
  187.     l1Trans.addChild(lgt1);
  188.     l2Trans.addChild(lgt2);
  189.  
  190.     PickRotateBehavior behavior1 =
  191.     new PickRotateBehavior(objRoot, canvas, bounds);
  192.     behavior1.setMode(PickTool.GEOMETRY);
  193.     behavior1.setTolerance(0.0f);
  194.     objRoot.addChild(behavior1);
  195.  
  196.     PickZoomBehavior behavior2 =
  197.     new PickZoomBehavior(objRoot, canvas, bounds);
  198.     behavior2.setMode(PickTool.GEOMETRY);
  199.     behavior2.setTolerance(0.0f);
  200.     objRoot.addChild(behavior2);
  201.  
  202.     PickTranslateBehavior behavior3 =
  203.     new PickTranslateBehavior(objRoot, canvas, bounds);
  204.     behavior3.setMode(PickTool.GEOMETRY);
  205.     behavior3.setTolerance(0.0f);
  206.     objRoot.addChild(behavior3);
  207.  
  208.     // Let Java 3D perform optimizations on this scene graph.
  209.     objRoot.compile();
  210.     
  211.     return objRoot;
  212.   }
  213.   
  214.   public PickText3DGeometry() {
  215.   }
  216.  
  217.     public void init() {
  218.     setLayout(new BorderLayout());
  219.     GraphicsConfiguration config =
  220.         SimpleUniverse.getPreferredConfiguration();
  221.     Canvas3D c = new Canvas3D(config);
  222.     add("Center", c);
  223.     
  224.     u = new SimpleUniverse(c);
  225.     BranchGroup scene = createSceneGraph(c);
  226.     
  227.     // This will move the ViewPlatform back a bit so the
  228.     // objects in the scene can be viewed.
  229.     u.getViewingPlatform().setNominalViewingTransform();
  230.     
  231.     u.addBranchGraph(scene);
  232.     }
  233.  
  234.     public void destroy() {
  235.     u.cleanup();
  236.     }
  237.   
  238.   //
  239.   // The following allows Text3DMotion to be run as an application
  240.   // as well as an applet
  241.     //
  242.   public static void main(String[] args) {
  243.     new MainFrame(new PickText3DGeometry(), 700, 700);
  244.   }
  245. }
  246.